<template>
    <section class="modlude">
        <el-form :inline="true" :model="formSearch" class="form_inline" label-width="90px">
            <!-- <el-form-item label="所属组织" size="small">
                <el-cascader ref="cascader" v-model="option1" :options="dataTree" :props="defaultProps2"
                    @change="handleChange" clearable>
                </el-cascader>
            </el-form-item> -->
            <el-form-item label="选择项目" size="small">
                <el-select v-model="formSearch.projectId" placeholder="请选择项目">
                    <el-option v-for="(item,index) in xqTree" :label="item.name" :value="item.id" :key="index">
                    </el-option>
                </el-select>
            </el-form-item>
            <!-- v-if="$root.btnControl.find(item=>item=='repair_time_config_view')" -->
            <el-form-item>
                <!-- v-if="$root.btnControl.find(item=>item=='view')" -->
                <el-button size="small"  @click="getlist">查 询</el-button>
            </el-form-item>
            <!-- v-if="$root.btnControl.find(item=>item=='add')" -->
            <el-form-item>
                <el-button size="small"  @click="addDialog=true">配 置</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="formData" style="width: 100%" >
            <!-- stripe @selection-change="handleSelectionChange" -->
            <!-- <el-table-column type="selection" width="55"></el-table-column> -->
            <el-table-column prop="" label="服务类型">
                <template slot-scope="scope">
                    {{scope.row.type==0?"巡查":scope.row.typeLink==1?"保洁":'设备运维'}}
                </template>
            </el-table-column>
            <el-table-column prop="" label="环节">
                <template slot-scope="scope">
                    {{scope.row.status==1?"自动派单":scope.row.status==2?"手动派单":scope.row.status==3?'开始':scope.row.status==4?"完成":"审核"}}
                </template>
            </el-table-column>
            <el-table-column prop="warningTime" label="预警时间">
                <template slot-scope="scope">
                    {{scope.row.status==2?'待派单 ':scope.row.status==3?'开始前 ':scope.row.status==4?'结束前':scope.row.status==5?'待审核':''}}
                    {{scope.row.status!=1?scope.row.warningTime:'- -'}}
                    {{scope.row.timeType==1 && scope.row.status!=1?' 月':scope.row.timeType==2 && scope.row.status!=1?' 天'
                    :scope.row.timeType==3 && scope.row.status!=1?' 小时':scope.row.timeType==4 && scope.row.status!=1?' 分钟':''}}
                </template>
            </el-table-column>
            <el-table-column prop="timeoutPeriod" label="报警时间">
                <template slot-scope="scope">
                    {{scope.row.status==2?'待派单 ':scope.row.status==5?'待审核':''}}
                    {{scope.row.status ==2 ||scope.row.status ==5 ?scope.row.timeoutPeriod:'- -'}}
                    {{scope.row.timeType==1 && (scope.row.status==2 || scope.row.status==5)?' 月':scope.row.timeType==2 && (scope.row.status==2 || scope.row.status==5)?' 天'
                    :scope.row.timeType==3 && (scope.row.status==2 || scope.row.status==5)?' 小时':scope.row.timeType==4 && (scope.row.status==2 || scope.row.status==5)?' 分钟':''}}
                </template>
            </el-table-column>
            <el-table-column  prop="" label="派单时间">
                <template slot-scope="scope">
                    {{scope.row.status==1?'开始前':''}}
                    {{scope.row.status==1?scope.row.warningTime:'- -'}}
                    {{scope.row.timeType==1 && scope.row.status==1?' 月':scope.row.timeType==2 && scope.row.status==1?' 天'
                    :scope.row.timeType==3 && scope.row.status==1?' 小时':scope.row.timeType==4 && scope.row.status==1?' 分钟':''}}
                </template>
            </el-table-column>
            <el-table-column label="操作" fixed="right" width=250>
                  <template slot-scope="scope">
                      <!-- v-if="$root.btnControl.find(item=>item=='edit')"  v-if="$root.btnControl.find(item=>item=='delete')"-->
                      <el-button type="warning"  size="small" @click="xiugai(scope.row,2)">修 改</el-button>
                      <el-button type="danger"  size="small" @click="deleInfor(scope.row.id)">删 除</el-button>
                  </template>
              </el-table-column>
        </el-table>
        <paging @changePage=handleCurrentPage :get-total='total'></paging>

        <!-- 添加 -->
        <el-dialog :title="Addoredit==1?'配置':Addoredit==2?'修改':'详情'" :visible.sync="addDialog" width="30%" @close="closeDialog" :close-on-click-modal="false">
            <div class="cont_box_right">
                <el-form label-position="right"  class="divform" label-width="100px" :model="formPush" ref='addList'>
                    <el-form-item label="选择项目" size="small"  :rules = "{ required: true}">
                        <el-select v-model="formPush.projectId" placeholder="请选择项目">
                            <el-option v-for="(item,index) in xqTree" :label="item.name" :value="item.id" :key="index">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="环节:" size="small" :rules = "{ required: true}">
                        <el-select v-model="formPush.status" placeholder="请选择环节" @change="getshiwuTypejieshu">
                            <el-option label="自动派单" :value="1"></el-option>
                            <el-option label="手动派单" :value="2"></el-option>
                            <el-option label="开始" :value="3"></el-option>
                            <el-option label="结束" :value="4"></el-option>
                            <el-option label="审核" :value="5"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="派单时间:" size="small" v-if="formPush.status ==1" :rules = "{ required: true}">
                        <span>开始前 </span>
                        <el-select style="margin-left:10px" v-model="formPush.warningTime" placeholder="">
                            <el-option v-for="(item,index) in numberTree" :label="item.name" :value="item.name"
                                :key="index">
                            </el-option>
                        </el-select>
                        <el-select style="margin-left:10px" @change="numberchange" v-model="formPush.timeType"
                            placeholder="">
                            <el-option label="分" :value="4"></el-option>
                            <el-option label="时" :value="3"></el-option>
                            <el-option label="日" :value="2"></el-option>
                            <!-- <el-option label="月" :value="1"></el-option> -->
                        </el-select>
                    </el-form-item>
                    <el-form-item label="预警时间:" size="small" v-if="formPush.status != 1" :rules = "{ required: true}">
                        <span>{{formPush.status==5?'待审核':formPush.status==4?'结束前':'开始前'}} </span>
                        <el-select style="margin-left:10px" v-model="formPush.warningTime" placeholder="">
                            <el-option v-for="(item,index) in numberTree" :label="item.name" :value="item.name"
                                :key="index">
                            </el-option>
                        </el-select>
                        <el-select style="margin-left:10px" @change="numberchange" v-model="formPush.timeType"
                            placeholder="">
                            <el-option label="分" :value="4"></el-option>
                            <el-option label="时" :value="3"></el-option>
                            <el-option label="日" :value="2"></el-option>
                            <!-- <el-option label="月" :value="1"></el-option> -->
                        </el-select>
                    </el-form-item>
                    <el-form-item label="报警时间:" size="small" v-if="formPush.status == 2 || formPush.status == 5" :rules = "{ required: true}">
                        <span>{{formPush.status==2?'开始前':'待审核'}} </span>
                        <el-select style="margin-left:10px" v-model="formPush.timeoutPeriod" placeholder="">
                            <el-option v-for="(item,index) in numberTree" :label="item.name" :value="item.name"
                                :key="index">
                            </el-option>
                        </el-select>
                        <el-select style="margin-left:10px" @change="numberchange" v-model="formPush.timeType"
                            placeholder="">
                            <el-option label="分" :value="4"></el-option>
                            <el-option label="时" :value="3"></el-option>
                            <el-option label="日" :value="2"></el-option>
                            <!-- <el-option label="月" :value="1"></el-option> -->
                        </el-select>
                    </el-form-item>
                </el-form>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button size="medium " @click="addDialog = false">取 消</el-button>
                <el-button size="medium " @click="getupdatelisttime">提 交</el-button>
            </div>
        </el-dialog>
       
    </section>
</template>

<script>
    import paging from "../paging"
    import paging1 from "../paging"
    import { updPatrolOptions,getPatrolOptionsList, addPatrolOptions, deletePatrolOptions, xqSelectList, orgTree } from '../../url/api';
    export default {
        data() {
            return {
                Addoredit:1,
                checkStrictly: false,
                bindRole: {},
                showmenuList: [],
                menuList: [],
                option1: [],
                xqTree: [],

                numberTree: [],
                hourTree:[],
                minuteTree:[],
                dayTree:[],

                isAddorUpdate: 1,//1添加、2修改
                formSearch: {
                    current: 1,
                    size: 10,
                    type:1
                },
                formSearch1: {
                    type: 1,
                },
                formData: [],//数据
                formData1: [],//数据
                deleBatch: [],//删除数据
                formUpdate: {},//修改表单
                total1: 0,//数据总数
                total: 0,//数据总数
                pages: 0,//页面总数
                addDialog: false,
                updateDialog: false,
                dialog: false,
                formPush: {
                    type: 1,
                    warningTime:1,
                    timeoutPeriod:1,
                    timeType:4
                },
                dataTree: [],
                defaultProps: {//树状图key定义
                    children: 'data',
                    label: 'name'
                },
                defaultProps2: {
                    children: 'children',
                    label: 'name',
                    value: 'id',
                    expandTrigger: 'hover', checkStrictly: true
                },
                
                typeIds: [],
                imageUrl: '',
            }
        },
        methods: {
            closeDialog(){
                this.formPush ={
                    type: 1,
                    warningTime:1,
                    timeoutPeriod:1,
                    timeType:4
                }
                //console.log("hahahaha");
                this.Addoredit = 1
            },
            xiugai(row,index){
                this.Addoredit = index
                this.formPush = row
                this.formPush.projectId = this.formPush.projectId*1
                this.addDialog = true
            },
            getshiwuTypejieshu(){},
            // getshiwuTypejieshu() {//服务类型列表
            //     let params = {
            //         type: this.formPush.type,
            //     }
            //     if (this.formPush.type == 0 && this.formPush.typeService == 0) {
            //         params = {
            //             type: this.formPush.type,
            //             typeService: this.formPush.typeService,
            //         }
            //     }
            //     if (this.formPush.type != 1) {
            //         shiwuTypejieshu(params).then((res) => {
                        
            //             if (res.data.ok) {
            //                 this.formData1 = res.data.data.filter((item) => {
            //                     return item
            //                 })
            //                 this.total1 = res.data.data.total
            //             } else {
            //                 this.$message(res.data.msg);
            //             }
            //         })
            //     }


            // },
            getupdatelisttime() {//添加配置预警超时

                // if(this.formPush.timeType == 1){
                //     if(this.formPush.warningTime > 12 || this.formPush.timeoutPeriod > 12){
                //         this.$message('当前日期单位与数字不符！')
                //         return
                //     }
                // }
                if(this.formPush.timeType == 2){
                    if(this.formPush.warningTime > 31 || this.formPush.timeoutPeriod > 31){
                        this.$message('当前日期单位与数字不符！')
                        return
                    }
                }
                if(this.formPush.timeType == 3){
                    if(this.formPush.warningTime > 24 || this.formPush.timeoutPeriod > 24){
                        this.$message('当前日期单位与数字不符！')
                        return
                    }
                }
                if(!this.formPush.projectId){
                    this.$message('请选择项目！')
                    return
                }
                if (!this.formPush.status) {
                    this.$message('请选择环节类型')
                    return
                }
                if(this.formPush.status!=2 && this.formPush.status !=5){
                     delete this.formPush.timeoutPeriod
                }
                
                ////console.log(this.formPush);
                let params = this.formPush
                
                if(this.Addoredit == 1){
                    addPatrolOptions(params).then(res => {
                    ////console.log(res)
                    if (res.data.ok) {
                        this.getlist()
                        this.$message('配置成功')
                    } else {
                        this.$message('配置失败')
                    }
                    this.addDialog = false
                })
                }else if(this.Addoredit == 2){
                    updPatrolOptions(params).then(res => {
                    ////console.log(res)
                    if (res.data.ok) {
                        this.getlist()
                        this.$message('修改成功')
                    } else {
                        this.$message('修改失败')
                    }
                    this.addDialog = false
                })
                // //console.log(this.formPush);
                //             this.$message('还没有修改接口！')
                //             this.addDialog = false
                }

                
            },

            getlist() {
                getPatrolOptionsList(this.formSearch).then((res) => {//查询预警超时
                    //console.log(res)
                    if (res.data.ok) {
                        this.formData = res.data.data.records.filter((item) => {
                            return item
                        })
                        this.total = res.data.data.total * 1
                    } else {
                        this.$message(res.data.msg);
                    }
                })
            },
            gettimetype(){

                for (var count1 = 1; count1 <= 60; count1++) {
                    let numberdata = {};
                    numberdata.name = count1
                    this.minuteTree.push(numberdata);
                    this.numberTree.push(numberdata)
                };
                //---时
                for (var count2 = 1; count2 <= 24; count2++) {
                    let numberdata = {};
                    numberdata.name = count2
                    this.hourTree.push(numberdata)
                };
                //---天
                for (var count3 = 1; count3 <= 31; count3++) {
                    let numberdata = {};
                    numberdata.name = count3
                    this.dayTree.push(numberdata)
                };
            },
            numberchange(event,item) {
                ////console.log(event);
                if(event==4){
                    this.numberTree = this.minuteTree
                }else if(event==3){
                    this.numberTree = this.hourTree
                    ////console.log( this.numberTree);
                }else if(event==2){
                    this.numberTree = this.dayTree
                }
            },
            getInit() {
                this.gettimetype()
                // this.getshiwuTypejieshu()
                this.getlist()
                xqSelectList({}).then((res) => {//项目选择列表
                    ////console.log(res)
                    if (res.data.ok) {
                        this.xqTree = res.data.data
                    }
                })
                let org_tree = {
                    name: '',
                    status: 1
                }
                orgTree(org_tree).then((res) => {//获取组织列表
                    ////console.log(res)
                    if (res.data.ok) {
                        this.dataTree = res.data.data
                    }
                })
            },
            handleChange(value) {
                if (value.length != 0) {
                    ////console.log(value)
                    this.formSearch.orgId = this.$refs.cascader.getCheckedNodes()[0].data.id
                } else {
                    this.formSearch.orgId = ''
                }
            },
           
            
           
            deleInfor(ids) {//删除
                let arrId = []
                if (ids) {
                    arrId.push(ids)
                } else {
                    if (this.deleBatch.length != 0) {
                        this.deleBatch.filter((item) => {
                            arrId.push(item.id)
                            return item
                        })
                    }
                }
                this.$confirm('确认删除吗？')
                    .then(_ => {
                        let params = arrId
                        deletePatrolOptions(params).then((res) => {
                            ////console.log(res)
                            if (res.data.ok) {
                                this.$message('删除成功');
                                this.getlist()
                            }
                        })
                    })
                    .catch(_ => { });
            },
            handleSelectionChange(val, self) {//多选
                this.deleBatch = val
            },
            handleCurrentPage(val) {//页码改变
                this.formSearch.current = val
                this.getlist()
            },
           
        },
        mounted() {
            this.getInit()
        },
        components: {
            paging,
            paging1
        }
    }
</script>
<style scoped>
.divform>>>.el-input__inner {
        max-width: 120px;
        min-width: 80px;
    }
</style>